<?php

namespace app\common\model;

use app\common\model\Base;
use think\Db;

class ProductRelation extends Base {

    public function product_relation_edit_submit($post, $id) {

        Db::startTrans();
        try {



            $product_ids = isset($post['product_ids']) ? $post['product_ids'] : [];
            $multiple = isset($post['multiple']) ? $post['multiple'] : [];
			$price = isset($post['price']) ? $post['price'] : [];


            db('product_relation')->where('p_id', $id)->delete();

            foreach ($product_ids as $value) {

                if (!empty($value) && is_numeric($value)) {

                    // 依次往relation表中添加数据 
                    db('product_relation')->insert([
                        'p_id' => $id,
                        'p_id_bc' => $value,
                        'multiple' => $multiple[$value],
                        'price' =>$price[$value]   //新增价格字段
                    ]);
                }
            }




            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $e->getMessage();
        }
    }
		//2019-08-05新增包材关联-商品列表下包材列表函数
   public function model_where(){
        
        $this->join('product p', 'p.id=a.p_id_bc');
       // $this->join('product_warehouse pw', 'pw.id=a.w_id');
        
        $this->field('a.*,'
                . 'p.name as product_title,p.image'); 
        $this->alias('a');        
        return $this;
    }
}
